package cn.tedu.cgb2110boot02.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

@WebServlet("/user/addServlet")
public class RequestServlet02 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("提交成功post");
        String user = request.getParameter("user");
        String pwd = request.getParameter("pwd");
        String age = request.getParameter("age");
        System.out.println(user+pwd+age);
        Connection c = null;
        PreparedStatement p =null;
        try {
            //导入jar包(在pom.xml中添加jar包的坐标依赖)
            //注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取链接
            String url="jdbc:mysql://localhost:3306/cgb2110?characterEncoding=utf8";
            c = DriverManager.getConnection(url, "root", "root");
            //获取传输器
            String sql = "insert into user values(?,?,?)";
            p = c.prepareStatement(sql);
            p.setObject(1,user);
            p.setObject(2,pwd);
            p.setObject(3,age);
            //执行SQL语句
            p.executeUpdate();
            System.out.println("成功");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //释放资源
            if (p!=null){
            try {
                p.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                p=null;
            }
            }if (c!=null){
            try {
                c.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                c=null;
            }
            }
        }



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("提交成功get");
        String deptno = request.getParameter("deptno");
        String deptname = request.getParameter("deptname");
        String deptloc = request.getParameter("deptloc");
        System.out.println(deptno+deptname+deptloc);
        Connection c =null;
        PreparedStatement p =null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/cgb2110?characterEncoding=utf8";
            c = DriverManager.getConnection(url, "root", "root");
            String sql = "insert into dept values(null,?,?,?)";
            p = c.prepareStatement(sql);
            p.setObject(1,deptno);
            p.setObject(2,deptname);
            p.setObject(3,deptloc);
            p.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (p!=null){
            try {
                p.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                p=null;
            }
            }
            if (c!=null){
            try {
                c.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                c=null;
            }
            }
        }

    }
}
